using GraphQL;
using GraphQL.Types;
using GraphQLDemo.Api.Extensions;
using GraphQLDemo.Domain;
using GraphQLDemo.Domain.Companies;
using GraphQLDemo.Domain.Contracts;
using GraphQLDemo.Domain.Orders;

namespace GraphQLDemo.Api.GroupTypes;

/// <summary>
/// 最外层组织根节点(唯一节点)
/// </summary>
[Authorize("Global")]
public sealed class QueryGraphType : ObjectGraphType
{
    public QueryGraphType(ICompanyRepository companyRepository, IContractRepository contractRepository, IOrderRepository orderRepository)
    {
        Name = "MainQuery";
        Description = "query top layer";

        Field<CompanyGraphType>("company")
            .Resolve(context =>
            {
                var company = new Company();
                return company;
            });
        Field<ListGraphType<CompanyPageGraphType>>("companyPage")
            .ResolveAsync(async context =>
            {
                var contracts = await companyRepository.GetListAsync();
                return contracts;
            });
        Field<IntGraphType>("companyPageCount")
            .ResolveAsync(async context =>
            {
                var contracts = await companyRepository.GetListAsync();
                return contracts.Count();
            });

        Field<ContractGraphType>("contract")
            .Resolve(context =>
            {
                var contract = new Contract()
                {
                    Name = "test"
                };
                return contract;
            });
        Field<ListGraphType<ContractPageGraphType>>("contractPage")
            .ResolveAsync(async context =>
            {
                var contracts = await contractRepository.GetListAsync();
                return contracts;
            });
        Field<IntGraphType>("contractPageCount")
            .ResolveAsync(async context =>
            {
                var contracts = await contractRepository.GetListAsync();
                return contracts.Count;
            });

        Field<OrderGraphType>("order")
            .Arguments(new QueryArguments()
            {
                new QueryArgument<GuidGraphType> { Name = "id", Description = "id of the human" },
            })
            .ResolveAsync(async context =>
            {
                var user = context.User;
                var userContext = context.UserContext as GraphQLUserContext;
                var id = context.GetArgument<Guid>("id");
                var order = await orderRepository.GetAsync(id);
                return order;
            })
            .Authorize()
            .ApplyPortal(this,PortalType.Client);

        Field<ListGraphType<OrderPageGraphType>>("orderPage")
            .Arguments(new QueryArguments()
            {
                new QueryArgument<GuidGraphType> { Name = "id", Description = "id of the human" },
            })
            .ResolveAsync(async context =>
            {
                var id = context.GetArgument<Guid>("id");
                var orders = await orderRepository.GetListAsync();
                return orders;
            });
        Field<IntGraphType>("orderPageCount")
            .Arguments(new QueryArguments()
            {
                new QueryArgument<GuidGraphType> { Name = "id", Description = "id of the human" },
            })
            .ResolveAsync(async context =>
            {
                var id = context.GetArgument<Guid>("id");
                var orders = await orderRepository.GetListAsync();
                return orders.Count();
            });
    }
}